package br.com.sgpv2.datasource.folha;

import java.util.List;

import net.sf.jasperreports.engine.JRDataSource;
import net.sf.jasperreports.engine.JREmptyDataSource;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.data.JRAbstractBeanDataSourceProvider;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;

import org.springframework.ui.Model;

import br.com.sgpv2.entity.folha.Folha;
import br.com.sgpv2.entity.movimentacao.Movimentacao;

public class RelatorioEmissaoDataSource extends JRAbstractBeanDataSourceProvider {

	public List<Movimentacao> movimentacoes;
	
	public RelatorioEmissaoDataSource() {
		super(Folha.class);
	}
	
	public RelatorioEmissaoDataSource(Folha folha, Model model) {
		this();
		this.movimentacoes = folha.getMovimentacoes();
		try {
			model.addAttribute("datasource", create(null));
		} catch (JRException e) {
			model.addAttribute("datasource", new JREmptyDataSource());
		}
		//csv, html, pdf and xls
		model.addAttribute("format", "pdf");
	}
	
	@Override
	public JRDataSource create(JasperReport arg0) throws JRException {
		return new JRBeanCollectionDataSource(movimentacoes);
	}

	@Override
	public void dispose(JRDataSource arg0) throws JRException {
		movimentacoes.clear();
		movimentacoes = null;
	}

}
